package cc.orange.mainView;


import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import cc.orange.R;
import cc.orange.base.BaseActivity;
import cc.orange.databinding.ActivityVideoBinding;
import cc.orange.entity.BaseEntity;
import cc.orange.http.ApiService;
import cc.orange.utils.StatusBarUtilView;
import cn.jzvd.JZDataSource;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;
import demo.smart.access.xutlis.util.ZXSharedPrefUtil;
import cc.orange.utils.ZXToastUtil;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.tendcloud.tenddata.TalkingDataSDK;
import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.DefaultCallback;
import com.xcheng.retrofit.HttpError;
import com.xcheng.retrofit.RetrofitFactory;

import android.os.Handler;

public class VideoActivity extends BaseActivity implements View.OnClickListener{

    private ActivityVideoBinding binding;
    private String url ,url_img;
    private String uid;
    private  String id;
    private int flag;
    private int zNum;
    private JzvdStd jzvdStd;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
//            long currentPositionWhenPlaying = MediaPlayerManager.instance().getCurrentPositionWhenPlaying();
//            long duration = MediaPlayerManager.instance().getDuration();
//            int l = (int)(((double)currentPositionWhenPlaying / duration) * 100);
//            Log.v("---->","cu--"+ currentPositionWhenPlaying +"---"+duration + "---" +l);
//            binding.videoProgress.setProgress(l);
//            handler.sendEmptyMessageDelayed(0,1000);
        }
    };

    private boolean isLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_video);
        ZXSharedPrefUtil  sharedPrefUtil = new ZXSharedPrefUtil();
        isLogin = sharedPrefUtil.getBool("isLogin_talk",false);
        url = getIntent().getStringExtra("video");
        url_img = getIntent().getStringExtra("img");
        uid = getIntent().getStringExtra("uid");
        id = getIntent().getStringExtra("id");
        zNum = getIntent().getIntExtra("zNum",1);
        flag = getIntent().getIntExtra("flag",1);
        binding.videoText2.setText(getIntent().getStringExtra("name"));
        binding.videoText3.setText(getIntent().getStringExtra("title"));

        Glide.with(this).load(url_img).into(binding.videoImg1);
        JZDataSource jzDataSource = new JZDataSource(url,"");
        jzDataSource.looping = true;
        binding.videoView.setUp(jzDataSource, Jzvd.SCREEN_NORMAL);
        binding.videoView.fullscreenButton.setVisibility(View.GONE);
        binding.videoView.startVideo();
        binding.msgaRel5.setOnClickListener(this);
        binding.videoImg2.setOnClickListener(this);
        binding.videoClick2.setOnClickListener(this);

        if(flag == 0){
            binding.videoClick2.setImageResource(R.drawable.icon_video_img2);
            binding.videoText1.setTextColor(getResources().getColor(R.color.color_fc3762));
        }else{
            binding.videoClick2.setImageResource(R.drawable.icon_video_img3);
            binding.videoText1.setTextColor(getResources().getColor(R.color.white));
        }
        binding.videoText1.setText(""+zNum);

//替换内核 在实例化之后立即调用
//        MediaPlayerManager.instance().setMediaPlayer(new ExoPlayer(VideoActivity.this));
//        // 设置视频地址和标题 并开始播放
//        binding.videoView.setUp(url);
////        binding.videoView.
//        binding.videoView.start();
//        MediaPlayerManager.instance().setLooping(true); //循环
//
//        binding.videoView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if(binding.videoStopIcon.getVisibility() == View.VISIBLE){
//                    MediaPlayerManager.instance().start();
//                    binding.videoStopIcon.setVisibility(View.GONE);
//                }else{
//                    MediaPlayerManager.instance().pause();
//                    binding.videoStopIcon.setVisibility(View.VISIBLE);
//                }
//
//            }
//        });
        handler.sendEmptyMessageDelayed(0,1000);
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        StatusBarUtilView.transparencyBar(VideoActivity.this);
        StatusBarUtilView.setLightStatusBar(VideoActivity.this, true, true);

        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) binding.msgaRel5.getLayoutParams();
        layoutParams.setMargins(30,getStatusBarHeight()+30,0,0);
        binding.msgaRel5.setLayoutParams(layoutParams);
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
        binding.videoStopIcon.setVisibility(View.GONE);
//        MediaPlayerManager.instance().start();
//        handler.sendEmptyMessageDelayed(0,1000);
        binding.videoView.goOnPlayOnResume();
        TalkingDataSDK.onPageBegin(this, getLocalClassName());
    }

    @Override
    protected void onPause() {
        super.onPause();
//        binding.videoStopIcon.setVisibility(View.VISIBLE);
//        MediaPlayerManager.instance().pause();
        handler.removeMessages(0);
//        binding.videoView.onStatePause();
        TalkingDataSDK.onPageEnd(this, getLocalClassName());
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Jzvd.releaseAllVideos();
//        MediaPlayerManager.instance().releasePlayerAndView(this);
    }

//    @Override
//    public void onBackPressed() {
//        if (Jzvd.backPress()) {
//            return;
//        }
//        super.onBackPressed();
//    }
//    @Override
//    protected void onPause() {
//        super.onPause();
//        Jzvd.releaseAllVideos();
//    }

    @Override
    public void onClick(View v) {
        if(!isLogin && v.getId() != R.id.msga_rel5){
            ZXToastUtil.showToast("请先登录");
            return;
        }
        switch (v.getId()){
            case R.id.msga_rel5:
                if (Jzvd.backPress()) {
                    return;
                }
                finish();
                break;
            case R.id.video_click2:
                likeVideo(id,uid,flag == 1 ? 0 : 1);
                break;
            case R.id.video_img2:
                binding.videoView.goOnPlayOnPause();
                Intent intent = new Intent(VideoActivity.this, MsgActivity.class);
                intent.putExtra("uid",uid);
                startActivity(intent);
                break;
        }
    }



    private void likeVideo(String video,String uid,int flags){
        RetrofitFactory.create(ApiService.class)
                .likeVideo(getTokens(),video,uid,""+flags)
                .enqueue(new DefaultCallback<BaseEntity>() {
                    @Override
                    public void onStart(Call<BaseEntity> call) {
                        showLoading_base(VideoActivity.this);
                    }

                    @Override
                    public void onError(Call<BaseEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<BaseEntity> call, BaseEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            flag = flags;
                            if(flags == 0){
                                zNum ++ ;
                                binding.videoClick2.setImageResource(R.drawable.icon_video_img2);
                                binding.videoText1.setTextColor(getResources().getColor(R.color.color_fc3762));
                            }else{
                                zNum -- ;
                                binding.videoClick2.setImageResource(R.drawable.icon_video_img3);
                                binding.videoText1.setTextColor(getResources().getColor(R.color.white));
                            }
                            binding.videoText1.setText(""+zNum);
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }
}